package com.zerocarbon.framework.common.result.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerocarbon.framework.common.utils.BeanPlusUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 分页数据展示类
 * @author wyf
 * 2020年7月10日
 * @param <T>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageVO<T> implements Serializable {

	private static final long serialVersionUID = -1513769781985860502L;
	
    /**
     * 当前页结果集
     */
    private List<T> list;
	
    private Long totalCount;			//总条数
    
    private Long totalPage;				//总页数
    
    private Long pageNo;				//当前第几页
    
    private Long pageSize;				//一页显示几条
    
    /**
     * 把IPage转化成PageVO
     * @param page	mybatisplus的IPage分数数据结构
     * @return
     */
    public static <T> PageVO<T> toBeanPageVO(IPage<T> page) {
		return new PageVO<T>(page.getRecords(), page.getTotal(), page.getPages(), page.getCurrent(), page.getSize());
	}
    
    /**
     * 把IPage转化成PageVO,并把泛型数据转化成destinationClass
     * @param page
     * @param destinationClass
     * @return
     */
    public static <E, T> PageVO<E> toBeanPageVO(IPage<T> page, Class<E> destinationClass) {
    	if (page == null || destinationClass == null)
    		return null;
    	List<E> newList = BeanPlusUtil.toBeanList(page.getRecords(), destinationClass);
		return new PageVO<E>(newList, page.getTotal(), page.getPages(), page.getCurrent(), page.getSize());
	}
    
    /**
     * 把PageVO的泛型数据转化成destinationClass
     * @param pageVO
     * @param destinationClass
     * @return
     */
    public static <E, T> PageVO<E> toBeanPageVO(PageVO<T> pageVO, Class<E> destinationClass) {
    	if (pageVO == null || destinationClass == null)
    		return null;
    	List<E> newList = BeanPlusUtil.toBeanList(pageVO.getList(), destinationClass);
		return new PageVO<E>(newList, pageVO.getTotalCount(), pageVO.getTotalPage(), pageVO.getPageNo(), pageVO.getPageSize());
	}
    
    /**
     * 把IPage的泛型数据转化成destinationClass
     * @param page
     * @param destinationClass
     * @return
     */
    public static <E, T> IPage<E> toBeanPage(IPage<T> page, Class<E> destinationClass) {
    	if (page == null || destinationClass == null)
    		return null;
    	IPage<E> newPage = new Page<>(page.getCurrent(), page.getSize());
    	newPage.setPages(page.getPages());
    	newPage.setTotal(page.getTotal());
    	newPage.setRecords(BeanPlusUtil.toBeanList(page.getRecords(), destinationClass));
    	return newPage;
    }

    /**
     * 构建分页返回数据结构
     * @param list
     * @param totalCount
     * @param totalPage
     * @param pageNo
     * @param pageSize
     * @return
     */
	public static <T> PageVO<T> createPageVO(List<T> list, Long totalCount, Long totalPage, Long pageNo, Long pageSize) {
		return new PageVO<T>(list, totalCount, totalPage, pageNo, pageSize);
	}
}
